<template>
	<div class="addRules">
		<!-- 规则 -->
		<el-form :model="addData" style="margin-left:20px;" :rules="rules" ref="ruleForm" label-position=right label-width="90px">
			<el-form-item label="规则名称:" prop="ruleName">
			  <el-input v-model="addData.ruleName"></el-input>
			</el-form-item>
			<el-form-item label="适用车场:" prop="parkId">
        <el-select v-model="addData.parkId" filterable placeholder="请选择">
          <el-option
            v-for="item in parkOptions"
            :key="item.id"
            :label="item.parkName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
     <!-- <el-form-item label-width="0" v-if="selectParkShow" class="parkList">
        <template v-for="item in Letter">
          <span :class="{active: actived == item}" @click="getAllPark(item)">{{item}}</span>
        </template>
        <span class="allSeletc" @click="ifAllParks = true;getAllPark()" :indeterminate="isIndeterminate">A-Z全选</span>
        <span class="allSeletc" @click="empty">清空</span>
        <hr/>
        <template>
          <el-checkbox-group class="parkNames" v-model="addData.parkIds" v-for="item in parkList">
            <el-checkbox :label="item.id" :disabled="ruleParkIds.indexOf(item.id) > -1" >{{ item.parkName }}</el-checkbox>
          </el-checkbox-group>
        </template>
      </el-form-item> -->
			<el-form-item label="预约费用:">
			  7:00 - 11:00 <el-input v-model="addData.timeSlotList[0].amount" size="mini" style="width: 100px;"></el-input>
        11:00 - 17:00 <el-input v-model="addData.timeSlotList[1].amount" size="mini" style="width: 100px;"></el-input>
			</el-form-item>

      <el-form-item label="车场分成:" prop="parkDivide">
        <el-input v-model.number="addData.parkDivide" size="mini" style="width: 55px;"></el-input> %
      </el-form-item>
    </el-form>
    <p style="text-align: center;margin-left: -20px;">
      <el-button @click="cancle">取消</el-button>
    	<el-button type="warning" @click="confirmAdd('ruleForm')">确认</el-button>
    </p>
	</div>
</template>

<script>
export default{
	name:'addRule',
	data(){
		return{
      checkList: [],
      actived: 'A',
      Letter: ["A","B","C","D","E","F","G","H","J","K","L","M","N","O","P","Q","R","S","T","W","X","Y","Z"],
      addData: {
        timeSlotList: [
          {
            reserveBeginTime: '07:00:00',
            reserveEndTime: '11:00:00'
          },
          {
            reserveBeginTime: '11:00:00',
            reserveEndTime: '17:00:00'
          }
        ],
        ifAllParks: 0,
        parkIds: []
      },
      parkOptions: [],
      parkList: [],
      selectParkShow: true,
			addShow:true,
      ifAllParks: false,
      isIndeterminate: true,
      ruleParkIds: [],
      rules: {
        ruleName: { required: true, message: '请输入规则名称', trigger: 'blur' },
        parkDivide: [
          { required: true, message: '请输入分成比例', trigger: 'blur' },
          { type: 'number', message: '分成比例为数字值', trigger: 'blur'},
          { pattern: /^(0|[1-9]\d?|100)$/, message: '范围在0-100', trigger: 'blur'}
        ],
        parkId: { required: true, message: '请选择停车场', trigger: 'blur' }
      },
		}
	},
	mounted(){
    this.getDurationRuleParkList();
    this.getAllPark('A');
    this.getNoReservePark();
	},
	methods:{
    getNoReservePark() {
      this.$axios({
        method: 'get',
        // url: '/park/park-info/get-no-reserve-park'
        url: '/park/park-info/get-no-park-reserve-rule'
      }).then(res => {
        if(res.code == 200) {
          this.parkOptions = res.data;
        }
      })
    },

    //根据首字母查询车场名称展示
    getAllPark(letter) {
      this.parkList = [];
      this.actived = letter;
      this.$axios({
        method: 'get',
        url: '/park/park-info/getParkList',
      }).then((res) => {
          res.data.forEach(item => {
            if(this.ifAllParks) {
              this.parkList.push(item);
            } else {
              if(item.parkNameFirstSpell == letter) {
                this.parkList.push(item)
              }
            }
          })
          //全选排除掉已有规则的车场
          if(this.ifAllParks) {
            let ruleIds = this.ruleParkIds
            let arr = [];
            res.data.forEach(item => {
              arr.push(item.id)
            })
            let diff = arr.filter(function (val) { return ruleIds.indexOf(val) === -1 });
            this.addData.parkIds = diff;
          }
          this.ifAllParks = false;
      })
    },

    //获取已有规则的停车场
    getDurationRuleParkList() {
      this.$axios({
        method: 'get',
        url: '/promotion/reserve-rule-park/getReserveRuleParkList',
      }).then((res) => {
        res.data.forEach((rulePark) => {
          this.ruleParkIds.push(rulePark.parkId);
        })
      })
    },

    //清空选中的车场
    empty() {
      this.addData.parkIds = [];
    },

		//确认新增规则接口
		confirmAdd(formName){
          if(this.addData.ifAllParks == 0) {
            // if(this.addData.parkIds.length == 0) {
            //   this.$message.warning('请选择停车场！')
            // } else {
              this.$refs[formName].validate((valid) => {
                if (valid) {
                  this.$axios({
                    method: 'post',
                    url: '/promotion/reserve-rule/addReserveRule',
                    data: this.addData
                  }).then((res) => {
                    if(res.code == 200) {
                      this.$message.success('新增规则成功');
                      this.$emit('closeDialog');
                      this.$emit('refresh')
                    } else {
                      this.$message.error(res.desc)
                    }
                  })
                } else {
                  return false;
                }
              });
            // }
          }
		},
    cancle() {
      this.$emit('closeDialog');
    }
	}
}
</script>

<style scoped>
.el-input{
	width: 300px;
}
.rul{
	width: 130px;
	margin:0 6px;
}
.adrul{
	font-size: 24px;
	color: red;
	float: right;
	margin-right: 50px;
}
.adrul:hover{
	cursor: pointer;
}
.ruletips{
	margin-left: 60px;
	color: #aaa;
}
.parkList {
  height: 200px;
  width: 670px;
  overflow: scroll;
}
.parkList span {
  padding: 6px;
}
.parkList span:hover {
  cursor: pointer;
}
.parkNames {
  display: inline-block;
  width: 150px;
  padding: 0;
  overflow: hidden;
}
.active {
  background-color: #1890FF;
  border-radius: 5px;
  color: #fff;
}
.allSeletc {
  background-color: #09CCDE;
  border-radius: 3px;
}
</style>
